#include "../Header/LogPrintBase.h"
#include "../Header/LogSet.h"


PrintBase::PrintBase()
{
    exit_.store(false);
    thread_cnt_.store(0);
}

void PrintBase::_getLogQuery(std::deque<LogItem> &que)
{
    un_lock_t lock(mtx_);
    if (!que_log_.empty()) {
        que_log_.swap(que);
    }
    else {
        cv_.wait(lock, [&]() {
            if (exit_)
                return true;
            else if (!que_log_.empty()) {
                que_log_.swap(que);
                return true;
            }
            return false;
        });
    }
}



str_t PrintBase::_setLogFormat(LogItem &data)
{
	std::stringstream ss;
    str_t time_str(LogSetting::getDateTimeStr(GET_POS(data).getTime()));
	ss << time_str << " [";
	if (LogSetting::getInstance().getFormat().print_file_)
        ss << GET_POS(data).getFile()<< " :";
	if (LogSetting::getInstance().getFormat().print_fun_)
        ss << GET_POS(data).getFun();
	if (LogSetting::getInstance().getFormat().print_line_)
        ss << ":" << GET_POS(data).getLine();
	ss << "] ";
	return ss.str();
}


void PrintBase::_getLogItem(LogItem & item)
{
	un_lock_t lock(mtx_);
	if (!que_log_.empty()) {
		item = std::move(que_log_.front());
		que_log_.pop_front();
	}
	else {
		cv_.wait(lock, [&]() {
			if (exit_)
				return true;
			else if (!que_log_.empty())
            {
				item = std::move(que_log_.front());
				que_log_.pop_front();
				return true;
			}
			return false;
		});
	}
	
}
